package bjut.core.netty;

import bjut.core.Message.RpcMessageDecoder;
import bjut.core.Message.RpcMessageEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 * @Author: zhuzhiming
 * @Date: 2022/4/14 20:50
 */
public class NettyServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    DefaultEventLoopGroup defaultEventLoopGroup;

    public NettyServerChannelInitializer(DefaultEventLoopGroup defaultEventLoopGroup) {
        this.defaultEventLoopGroup = defaultEventLoopGroup;
    }

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        // pipeline.addLast(new IdleStateHandler(60, 0, 0, TimeUnit.SECONDS))
        pipeline.addLast(new RpcMessageEncoder())
                .addLast(new RpcMessageDecoder())
                .addLast(defaultEventLoopGroup, new NettyRpcServiceHandler());
    }
}
